package com.yb.web.shoper;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yb.base.web.BaseAction;
import com.yb.bussiness.goods.CategoryService;
import com.yb.bussiness.goods.CategoryServiceImpl;
import com.yb.bussiness.shoper.TpBugRecordService;
import com.yb.bussiness.shoper.TpBuyRecordServiceImpl;
import com.yb.domain.goods.TgCategory;
import com.yb.domain.goods.TpBuyRecord;
import com.yb.domain.member.TsSysuser;
import com.yb.util.comm.Contants;
import com.yb.util.comm.RequestUtil;
import com.yb.util.framework.PageUtil;

public class TpBuyRecordAction extends BaseAction {
	private TpBugRecordService service;
	private TpBuyRecord tpRecord;
	private  List<TgCategory> catrows;
	private CategoryService categoryService;
	private int total;
	@SuppressWarnings("unchecked")
	private List rows;

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public List getRows() {
		return rows;
	}

	public void setRows(List rows) {
		this.rows = rows;
	}

	public TpBuyRecordAction() {
		service = new TpBuyRecordServiceImpl();
		categoryService = new CategoryServiceImpl();
	}
	/**
	 * 显示拼购信息列表出错
	 * 
	 * @return
	 */
	public String pinggourecordlist() {
		try {
			catrows = categoryService.findAll(1, 50);
		} catch (Exception e) {
			log.error("显示拼购记录管理出错", e);
		}
		return "pinggourecordlist";
	}
	
	/**
	 * 查询
	 * 
	 * @return
	 */
	public String search() {
		String page = RequestUtil.getParam(request, "page", "");
		String row = RequestUtil.getParam(request, "rows", "");
		String createTime_startTime = RequestUtil.getParam(request, "createTime_startTime", "");
		String createTime_endTime = RequestUtil.getParam(request, "createTime_endTime", "");
		String partTime_startTime = RequestUtil.getParam(request, "partTime_startTime", "");
		String partTime_endTime = RequestUtil.getParam(request, "partTime_endTime", "");
		String categoryId = RequestUtil.getParam(request, "categoryId", "");
		String brandId = RequestUtil.getParam(request, "brandId", "");
		TsSysuser tsSysuser = (TsSysuser) RequestUtil.getLoginUserFromSession(
				request, Contants.SESSION_USER);

		Map<String, String> parameMap = new HashMap<String, String>();
		if ("3".equals(tsSysuser.getType())) {
			parameMap.put("userid", tsSysuser.getId().toString());
		}
		parameMap.put("page", page);
		parameMap.put("row", row);
		parameMap.put("createTime_startTime", createTime_startTime);
		parameMap.put("createTime_endTime", createTime_endTime);
		parameMap.put("partTime_startTime", partTime_startTime);
		parameMap.put("partTime_endTime", partTime_endTime);
		parameMap.put("categoryId", categoryId);
		parameMap.put("brandid", brandId);
		int count = service.count1(parameMap);
		if (count != 0) {
			total = count;
			List<TpBuyRecord> aclist = service.findByParameMap1(parameMap);
			rows = aclist;
		}
		return Contants.JSONSTRING_RESULT_NAME;
	}
	/**
	 * 业主拼购记录查询
	 * @return
	 */
	public String search4Owner(){
		int page = RequestUtil.getParam(request, "page", 1);
		int row = RequestUtil.getParam(request, "rows", 10);
		TsSysuser tsSysuser = (TsSysuser) RequestUtil.getLoginUserFromSession(
				request, Contants.SESSION_USER);
		Map<String, String> parameMap = new HashMap<String, String>();
		parameMap.put("userid", tsSysuser.getId().toString());
		PageUtil pu = service.getList(parameMap, page, row);
		total = pu.getTotalRecords();
		rows = pu.getList();
		return Contants.JSONSTRING_RESULT_NAME;
	}
	
	public String search4OwnerById(){
		String id = RequestUtil.getParam(request, "id", "0");
		String brandName =RequestUtil.getParam(request, "brandName", "0");
		tpRecord = service.findById(id);
		tpRecord.setBrandName(brandName);
		return "yepinggourecordInfo";
	}
	
	public String savePinggou4Owner(){
		tpRecord.setModifyTimes(1);
		tpRecord.setAutding("0");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date = sdf.parse(tpRecord.getPartTime2()+"-01 00:00:00");
			tpRecord.setPartTime(date);
		} catch (ParseException e) {
			log.error("业主后台修改拼购记录,保存失败", e);
			e.printStackTrace();
		}
		service.saveOrUpdate(tpRecord);
		return "yepinggourecordlist";
	}
	/**
     * 查询
     * 
     * @return
     */
    public String searchpinggourecord() {
        String page = RequestUtil.getParam(request, "page", "");
        String row = RequestUtil.getParam(request, "rows", "");
        String createTime_startTime = RequestUtil.getParam(request, "createTime_startTime", "");
        String createTime_endTime = RequestUtil.getParam(request, "createTime_endTime", "");
        String partTime_startTime = RequestUtil.getParam(request, "partTime_startTime", "");
        String partTime_endTime = RequestUtil.getParam(request, "partTime_endTime", "");
        String categoryId = RequestUtil.getParam(request, "categoryId", "0");
        String brandName = RequestUtil.getParam(request, "brandName", "");
        String autding = RequestUtil.getParam(request, "autding", "");
        Map<String, String> parameMap = new HashMap<String, String>();
        parameMap.put("page", page);
        parameMap.put("row", row);
        parameMap.put("createTime_startTime", createTime_startTime);
        parameMap.put("createTime_endTime", createTime_endTime);
        parameMap.put("partTime_startTime", partTime_startTime);
        parameMap.put("partTime_endTime", partTime_endTime);
        parameMap.put("categoryId", categoryId);
        parameMap.put("brandName", brandName);
        parameMap.put("autding", autding);
        Map<String, Object> map = service.searchpinggourecord(parameMap);
        rows = (List<TpBuyRecord>)map.get("list");
        total = Integer.parseInt(map.get("total").toString());
        return Contants.JSONSTRING_RESULT_NAME;
    }

    
    
	
    public String loadpinggourecord(){
	    PrintWriter out;
        try{
            response.reset();
            response.setCharacterEncoding("utf-8");
            out = response.getWriter();
            Integer count = service.loadpinggourecord();
            out.write(count+"");
        }catch (Exception e) {
            log.error("加载新增的申请", e);
        }
        return null;
	}
    
	/**
	 * 删除
	 * 
	 * @return
	 */
	public String deleteById() {
		PrintWriter out = null;
		try {
			out = response.getWriter();
			String idStr = RequestUtil.getParam(request, "ids", "");
			boolean isboolean = service.deleteById(idStr);
			out.println("{result:" + isboolean + "}");
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 审核
	 * 
	 * @return
	 */
	public String updateMessageById() {
		PrintWriter out = null;
		try {
			out = response.getWriter();
			String idStr = RequestUtil.getParam(request, "id", "");
			String autding = RequestUtil.getParam(request, "autding", "");
			boolean isboolean = service.updateAutingId(idStr, autding);
			out.println("{result:" + isboolean + "}");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 增加
	 * 
	 * @return
	 */
	public String saveOrUpdate() {
		try {
			if(tpRecord!=null){
				tpRecord.setName(tpRecord.getName().trim());
				tpRecord.setCreateTime(new Date());
			}
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = sdf.parse(tpRecord.getPartTime2()+"-01 00:00:00");
			tpRecord.setPartTime(date);
			
			boolean isboolean = service.saveOrUpdate(tpRecord);
			if (isboolean)
				response.getWriter().write("<script>parent.addResult1('1')</script>");
			else
				response.getWriter().write("<script>parent.addResult1('2')</script>");
		} catch (Exception e) {
			e.printStackTrace();
			log.error(" TpBuyRecordAction saveOrUpdate error ", e);
			try {
				response.getWriter().write("<script>parent.addResult1('3')</script>");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}

	public TpBugRecordService getService() {
		return service;
	}

	public void setService(TpBugRecordService service) {
		this.service = service;
	}

	public TpBuyRecord getTpRecord() {
		return tpRecord;
	}

	public void setTpRecord(TpBuyRecord tpRecord) {
		this.tpRecord = tpRecord;
	}

	public List<TgCategory> getCatrows() {
		return catrows;
	}

	public void setCatrows(List<TgCategory> catrows) {
		this.catrows = catrows;
	}

}
